package com.yangjun.service.report;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangjun.common.ServerResponse;
import com.yangjun.common.comparator.TargetItemComparator;
import com.yangjun.common.comparator.TargetItemComparatorByTargetArr;
import com.yangjun.common.excel.DataBaseExcelContext;
import com.yangjun.common.excel.ExcelExportResult;
import com.yangjun.dao.mybatis.mapper.report.DvReportSearchTmpMapper;
import com.yangjun.dao.mybatis.mapper.report.ReportDownloadStatusMapper;
import com.yangjun.dao.mybatis.mapper.report.ReportExportStatusMapper;
import com.yangjun.dao.mybatis.mapper.report.ReportImportStatusMapper;
import com.yangjun.dao.mybatis.mapper.report.TmallRxReportExportMapper;
import com.yangjun.entity.report.DvExcelFieldDefinitionEntity;
import com.yangjun.entity.report.DvReportDownloadStatusEntity;
import com.yangjun.entity.report.DvReportExportStatus;
import com.yangjun.entity.report.DwTmallRxEntity;
import com.yangjun.po.report.TmallRxReportExportPO;
import com.yangjun.service.permission.ReportExportPermissionService;
import com.yangjun.utils.DvDateUtils;
import com.yangjun.utils.DvUtils;
import com.yangjun.utils.POIExcelUtils;
import com.yangjun.vo.report.TMallRxReportExportVO;
import com.yangjun.vo.report.TMallRxReportManageVO;

@Service
public class TMallRxReprotExportService {
	@Value("${excel.uploadPath}")
    private String uploadPath;
	@Value("${excel.downloadPath}")
    private String downloadPath;
	@Autowired
	private DataBaseExcelContext context;
	
	private final static String reporttype = "tmallRx";
	@Autowired
	private TmallRxReportExportMapper tmallRxReportExportMapper;
	@Autowired
	private ReportExportStatusMapper reportExportStatusMapper;
	@Autowired
	private ReportImportStatusMapper reportImportStatusMapper;
	@Autowired
	private DvReportSearchTmpMapper dvReportSearchTmpMapper;
	@Autowired
	private ReportCommonService reportCommonService;
	@Autowired
	private ReportExportPermissionService reportExportPermissionService;
	@Autowired
	private ReportDownloadStatusMapper reportDownloadStatusMapper;
	/**
	 * @Description 获取维度信息
	 * @return
	 * @author yangjun
	 * @Date 2017年7月5日 上午11:50:06
	 */
	//fetchType(取数类型) : 临时(temporary)订阅(subscription)
	public Object getDeminsion(String reportid,String userid){
		TMallRxReportExportVO tmrxreVo = null;
		DvReportExportStatus dvReportExportStatus = reportExportStatusMapper.queryReportStatusByReportId(reportid);
		
		String[] targetSelectData  = null;
		//修改报表排序号的字段顺序
		List<Map<String,Object>> sortTargetSelectData=Lists.newArrayList();
		String reportName = "天猫Rx单-";
		String uploadFileId = "";
		String uploadFileName = "";
		String searchName = "clientName";
		//取数类型(默认临时取数)
		String fetchType = "temporary";
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, 0);
		String endDate = DvDateUtils.format(calendar.getTime(),"yyyy-MM-dd HH:mm");
		calendar.add(Calendar.HOUR_OF_DAY, -1);
		String beginDate = DvDateUtils.format(calendar.getTime(),"yyyy-MM-dd HH:mm");
		Boolean modify = dvReportExportStatus!=null?true:false;

		if(dvReportExportStatus!=null){
			tmrxreVo = JSON.parseObject(dvReportExportStatus.getReportparam(), TMallRxReportExportVO.class);
			fetchType = tmrxreVo.getFetchType();
			beginDate = tmrxreVo.getBeginDate();
			endDate = tmrxreVo.getEndDate();
			targetSelectData = tmrxreVo.getTarget();
			reportName = tmrxreVo.getReportName();
			uploadFileId = tmrxreVo.getUploadFileId();
			if(StringUtils.isNotBlank(uploadFileId)){
				uploadFileName = reportImportStatusMapper.queryReportStatusByUUID(uploadFileId).getReportName();
			}
			searchName = tmrxreVo.getSearchName();
		}
		Map<String,Object> demensionMap = Maps.newHashMap();
		List<DvExcelFieldDefinitionEntity> excelFieldDefinitionList = reportCommonService.getExcelFieldDefinitionList(reporttype);

		//获取每个报表的指标分类
		Map<String,String> tmpCategoryMap = reportCommonService.queryCategory(reporttype);
		List<Map<String,String>> categoryList = Lists.newArrayList();
		
		Map<String,List<Map<String,Object>>> targetMap = Maps.newHashMap();
		List<Map<String,Object>> targetList =null;
		for(Map.Entry<String,String> categoryEntry: tmpCategoryMap.entrySet()){
			//初始化指标分类数组
			Map<String,String> categoryMap = Maps.newHashMap();
			categoryMap.put("name", categoryEntry.getKey());
			categoryMap.put("value", categoryEntry.getValue());
			categoryList.add(categoryMap);
			//初始化指标分类内容
			targetList = Lists.newArrayList();
			targetMap.put(categoryEntry.getKey(),targetList);
		}
		demensionMap.put("category", categoryList);
		//查询有权限显示的报表字段
		String[] allowFieldArr = reportExportPermissionService.getReportAllowField(userid, reporttype);
		if(allowFieldArr!=null&&allowFieldArr.length>0){
			for(DvExcelFieldDefinitionEntity excelFieldDefinitionEntity : excelFieldDefinitionList){
				//权限控制能够显示的字段，如果查询不到对应角色的权限直接不显示字段。
				List<String> allowFieldList = Arrays.asList(allowFieldArr);
				if(!allowFieldList.contains(excelFieldDefinitionEntity.getEntityName())){
					continue;
				}
				//封装默认的指标格式
				String categoryId = excelFieldDefinitionEntity.getCategoryId();
				if(StringUtils.isNotBlank(categoryId)){
					String entityName = excelFieldDefinitionEntity.getEntityName();
					String title = excelFieldDefinitionEntity.getTitle();
					int sort = excelFieldDefinitionEntity.getFieldSort();
					Map<String,Object> itemMap = Maps.newHashMap();
					itemMap.put("value", entityName);
					itemMap.put("name", title);
					itemMap.put("sort", sort);
					itemMap.put("checked", false);
					if(targetSelectData!=null){
						for(int i=0,len=targetSelectData.length;i<len;i++){
							if(entityName.equals(targetSelectData[i])){
								itemMap.put("checked", true);
								//修改报表排序号的字段顺序
								sortTargetSelectData.add(itemMap);
							}
						}
					}
					targetList = targetMap.get(categoryId);
					targetList.add(itemMap);
				}
			}
			for(Map.Entry<String,List<Map<String,Object>>> targetItemEntry : targetMap.entrySet()){
				Collections.sort(targetItemEntry.getValue(),new TargetItemComparator());
			}
		}
//		demensionMap.put("beginDate", beginDate);
//		demensionMap.put("endDate", endDate);
		demensionMap.put("uploadFileName", uploadFileName);
		demensionMap.put("uploadFileId", uploadFileId);
		demensionMap.put("reportName",reportName);
		//取数类型
		demensionMap.put("fetchType",fetchType);
		demensionMap.put("reporttype", reporttype);
		demensionMap.put("target", targetMap);
		demensionMap.put("update", "每天整点半");
		Collections.sort(sortTargetSelectData, new TargetItemComparatorByTargetArr(targetSelectData));
		demensionMap.put("sortTarget", sortTargetSelectData);
		//查询日期
		demensionMap.put("dateMethod", queryDateMethod(modify,beginDate,endDate));
		
		String[] searchWayNameArr = {"客户昵称","客户编码"};
		String[] searchWayValueArr = {"clientName","clientCode"};
		List<Map<String,String>> searchWayList = Lists.newArrayList();
		for(int i = 0,len=searchWayNameArr.length;i<len;i++){
			Map<String,String> searchMap = Maps.newHashMap();
			searchMap.put("searchWayName", searchWayNameArr[i]);
			searchMap.put("searchWayValue", searchWayValueArr[i]);
			searchWayList.add(searchMap);
		}
		demensionMap.put("search", searchWayList);
		demensionMap.put("searchName", searchName);
		
		reportCommonService.deleteUploadReport(userid);
		return ServerResponse.createBySuccess(demensionMap);
	}
	private Object queryDateMethod(Boolean modify,String beginDate,String endDate){
		Map<String,Map<String,Object>> resultMap = Maps.newHashMap();
		String[] fetchTypeArr = {"temporary","subscription"};
		for(String fetchTypeItem : fetchTypeArr ){
			if("temporary".equals(fetchTypeItem)){
				//临时取数
				resultMap.put(fetchTypeItem, queryDateOfTemporaryWay(beginDate,endDate));
			}else if("subscription".equals(fetchTypeItem)){
				//订阅报表
				resultMap.put(fetchTypeItem, queryDateOfSubscriptionWay(modify,beginDate,endDate));
			}
		}
		return resultMap;
	}

	private Map<String,Object> queryDateOfSubscriptionWay(Boolean modify,String beginDate,String endDate){
		Map<String,Object> resultMap = Maps.newHashMap();
		resultMap.put("name","最近一个小时");
		resultMap.put("beginDate", beginDate);
		resultMap.put("endDate", endDate);
		return resultMap;

	}
	private Map<String,Object> queryDateOfTemporaryWay(String beginDate,String endDate){
		Map<String,Object> resultMap = Maps.newHashMap();
		resultMap.put("beginDate", beginDate);
		resultMap.put("endDate", endDate);
		return resultMap;
	}
	
	
	public Object queryData(String reportid){
		DvReportExportStatus dvReportExportStatus = reportExportStatusMapper.queryReportStatusByReportId(reportid);
		TMallRxReportExportVO tmrxreVo = JSON.parseObject(dvReportExportStatus.getReportparam(), TMallRxReportExportVO.class);
		return queryData(tmrxreVo);
	}
	
	public Object queryData(TMallRxReportExportVO tmrxreVo){
		Map<String,Object> resultMap = Maps.newHashMap();
		String[] targetArr = tmrxreVo.getTarget();
		List<String> targetList = Arrays.asList(targetArr);
		Long start = System.currentTimeMillis();
		List<DvExcelFieldDefinitionEntity>  reportFieldDefinition= reportCommonService.getExcelFieldDefinitionList(reporttype);
		//装载表格数据
		List<List<String>> tableList = Lists.newArrayList();
		List<String> titleList = Lists.newArrayList();
		for(String item : targetList){
			for(DvExcelFieldDefinitionEntity definition : reportFieldDefinition){
				String name = definition.getEntityName();
				if(item.equals(name)){
					titleList.add(definition.getTitle());
				}
			}
		}
		tableList.add(titleList);
		System.out.println("1."+(System.currentTimeMillis()-start)/1000);

		//查找附件内容作为搜索条件
		if(StringUtils.isNotBlank(tmrxreVo.getUploadFileId())){
			int partSize=1000;
			List<String> searchItemList = Lists.newArrayList();
			String fileName = reportImportStatusMapper.queryReportStatusByUUID(tmrxreVo.getUploadFileId()).getFileName();
			File uploadFile = new File(uploadPath+fileName);
			FileInputStream readStream = null;
			try {
				readStream = new FileInputStream(uploadFile);
				List<List<Object>> datas =POIExcelUtils.readExcel(readStream, 0);
				for(int i=0,len=datas.size();i<len;i++){
					if(i>0&&datas.get(i).get(0)!=null){
						searchItemList.add(datas.get(i).get(0).toString());
					}
				}
				dvReportSearchTmpMapper.deleteDvReportSearchTmp(tmrxreVo.getUserid());
				if(searchItemList.size()>partSize){
					int part = searchItemList.size()/partSize;
					for(int i=0;i<part;i++){
						List<String> tmpList  = searchItemList.subList(0, partSize);
						dvReportSearchTmpMapper.insertDvReportSearchTmpByBatch(tmrxreVo.getUserid(), tmpList);
						searchItemList.subList(0, partSize).clear();
					}
				}
				if(searchItemList.size()>0){
					dvReportSearchTmpMapper.insertDvReportSearchTmpByBatch(tmrxreVo.getUserid(), searchItemList);
				}
				tmrxreVo.setSearchValue(searchItemList.size()>0?searchItemList.toArray(new String[searchItemList.size()]):null);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		System.out.println("2."+(System.currentTimeMillis()-start)/1000);
		TmallRxReportExportPO tmallRxReportExportPO = new TmallRxReportExportPO();
		BeanUtils.copyProperties(tmrxreVo,tmallRxReportExportPO);
		
		List<DwTmallRxEntity> list = tmallRxReportExportMapper.queryData(tmallRxReportExportPO,1,10);
		JSONArray objectArr = JSON.parseArray(JSONObject.toJSONString(list));
		for(int i = 0,len = objectArr.size() ; i<len ; i++){
			JSONObject object = objectArr.getJSONObject(i);
			List<String> itemList = Lists.newArrayList();
			for(String item : targetList){
				itemList.add(object.getString(item)==null?"":object.getString(item));
			}
			tableList.add(itemList);
		}
		System.out.println("3."+(System.currentTimeMillis()-start)/1000);
		resultMap.put("tableData",tableList );
		resultMap.put("rownum",list.size()>0?list.get(0).getCountNum():0);
		return ServerResponse.createBySuccess(resultMap);
	}
	
	
	public Object addDownloadTask(String reportid,HttpServletRequest request){
		DvReportExportStatus dvReportExportStatus = reportExportStatusMapper.queryReportStatusByReportId(reportid);
		TMallRxReportExportVO tmrxreVo = JSON.parseObject(dvReportExportStatus.getReportparam(), TMallRxReportExportVO.class);
		return download(tmrxreVo,request);
	}
	
	public Object addDownloadTask(TMallRxReportExportVO tmrxreVo,HttpServletRequest request){
		if(reportDownloadStatusMapper.queryReportByName(tmrxreVo.getUserid(), tmrxreVo.getReportName())>0){
			return ServerResponse.createByErrorMessage("已存在相同名称的报表，请重新命名!");
		}else{
			return download(tmrxreVo,request);
		}
	}
	
	public Object download(TMallRxReportExportVO tmrxreVo,HttpServletRequest request){
		context.initDBExcelDefinitionReader("export");
		TmallRxReportExportPO tmrxrePo= new TmallRxReportExportPO();
		String[] targetArr = tmrxreVo.getTarget();
		List<String> targetList = Arrays.asList(targetArr);
		//mybatis无法判断数组长度
		List<String> searchItemList = Lists.newArrayList();
			if(StringUtils.isNotBlank(tmrxreVo.getUploadFileId())){
				int partSize=1000;
				String uploadFileName = reportImportStatusMapper.queryReportStatusByUUID(tmrxreVo.getUploadFileId()).getFileName();
				File uploadFile = new File(uploadPath+uploadFileName);
				FileInputStream readStream = null;
				try {
					readStream = new FileInputStream(uploadFile);
					List<List<Object>> datas =POIExcelUtils.readExcel(readStream, 0);
					for(int i=0,len=datas.size();i<len;i++){
						if(i>0&&datas.get(i).get(0)!=null){
							searchItemList.add(datas.get(i).get(0).toString());
						}
					}
					dvReportSearchTmpMapper.deleteDvReportSearchTmp(tmrxreVo.getUserid());
					if(searchItemList.size()>partSize){
						int part = searchItemList.size()/partSize;
						for(int i=0;i<part;i++){
							List<String> tmpList  = searchItemList.subList(0, partSize);
							dvReportSearchTmpMapper.insertDvReportSearchTmpByBatch(tmrxreVo.getUserid(), tmpList);
							searchItemList.subList(0, partSize).clear();
						}
					}
					if(searchItemList.size()>0){
						dvReportSearchTmpMapper.insertDvReportSearchTmpByBatch(tmrxreVo.getUserid(), searchItemList);
					}
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
			}
			tmrxreVo.setSearchValue(searchItemList.size()>0?searchItemList.toArray(new String[searchItemList.size()]):null);
			BeanUtils.copyProperties(tmrxreVo, tmrxrePo);
			//用户ID，UUID组成下载ID
			String downloadId = tmrxreVo.getUserid()+UUID.randomUUID().toString().replaceAll("-", "");
			String downloadReportName = tmrxreVo.getReportName();
			String fileName =downloadId+downloadReportName+".xlsx";
			
			DvReportDownloadStatusEntity dvReportDownloadStatusEntity = new DvReportDownloadStatusEntity();
			dvReportDownloadStatusEntity.setDownloadId(downloadId);
			dvReportDownloadStatusEntity.setFileName(fileName);
			dvReportDownloadStatusEntity.setReportName(downloadReportName);
			dvReportDownloadStatusEntity.setReportType(reporttype);
			dvReportDownloadStatusEntity.setUserid(tmrxreVo.getUserid());
			dvReportDownloadStatusEntity.setUsername(tmrxreVo.getUsername());
			dvReportDownloadStatusEntity.setUserrealname(tmrxreVo.getUserrealname());
			dvReportDownloadStatusEntity.setTargetName(JSON.toJSONString(reportCommonService.getTargetNameList(targetArr, reporttype)));
			dvReportDownloadStatusEntity.setIp(DvUtils.getIp(request));
			
			int rownum = tmallRxReportExportMapper.queryCount(tmrxrePo);
			if(reportDownloadStatusMapper.insertReport(dvReportDownloadStatusEntity)>0){
				DownloadTask downloadTask = new DownloadTask(fileName,targetList,rownum, tmrxrePo, dvReportDownloadStatusEntity);
				new Thread(downloadTask).start();  
				return ServerResponse.createBySuccessMessage("加入下载列表成功！");
			}else{
				return ServerResponse.createByErrorMessage("加入下载列表失败！");
			}
	}
		
		class DownloadTask implements Runnable{
			public  boolean flag = true;
			private String fileName;
			private Integer rownum;
			private List<String> targetList;
			private TmallRxReportExportPO tmrxrePo;
			private DvReportDownloadStatusEntity dvReportDownloadStatusEntity;
			public DownloadTask(String fileName,List<String> targetList,int rownum,TmallRxReportExportPO tmrxrePo,DvReportDownloadStatusEntity dvReportDownloadStatusEntity){
				this.fileName = fileName;
				this.rownum = rownum;
				this.tmrxrePo = tmrxrePo;
				this.targetList = targetList;
				this.dvReportDownloadStatusEntity = dvReportDownloadStatusEntity;
			}
			public void stopThread(){
				flag = false;
			}
			@Override
			public void run() {
				while (flag){
					OutputStream ops = null;
					ExcelExportResult exportResult = null;
					try {
						ops = new FileOutputStream(downloadPath+fileName);
						exportResult = context.createEmptyExcelForPart(reporttype,DwTmallRxEntity.class,targetList);
						int pageSize = 40000;
						int totalPage = (int)Math.ceil(rownum/pageSize);
						totalPage += rownum%pageSize>0?1:0; 
						for(int i=1;i<=totalPage;i++){
							exportResult.append(tmallRxReportExportMapper.queryData(tmrxrePo, i, pageSize));
						}
					Workbook workbook = exportResult.build();
					workbook.write(ops);
					ops.close();
					workbook.close();
					dvReportDownloadStatusEntity.setEndDate(new Date());
					dvReportDownloadStatusEntity.setTimeConsume(Math.round(dvReportDownloadStatusEntity.getEndDate().getTime()-dvReportDownloadStatusEntity.getBeginDate().getTime())/1000);
					dvReportDownloadStatusEntity.setDownloadFlag(1);
					reportDownloadStatusMapper.updateReportById(dvReportDownloadStatusEntity);
					this.stopThread();
					} catch (Exception e) {
						System.out.println("TMallRxReprot awaitTermination interrupted: " + e);  
						this.stopThread();
					}
				}
			}
		}
	public Object addOrUpdateReport(TMallRxReportExportVO tmrxreVo){
		String reportid = tmrxreVo.getReportid();
		List<String> targetNameList = Lists.newArrayList();
		String[] targetArr = tmrxreVo.getTarget();
		List<DvExcelFieldDefinitionEntity> excelFieldDefinitionList = reportCommonService.getExcelFieldDefinitionList(reporttype);
		for(String target : targetArr){
			for(DvExcelFieldDefinitionEntity dvExcelFieldDefinitionItem : excelFieldDefinitionList){
				String entityName = dvExcelFieldDefinitionItem.getEntityName();
				String targetName = dvExcelFieldDefinitionItem.getTitle();
					if(target.equals(entityName)){
						targetNameList.add(targetName);
					}
			}
		}
		TMallRxReportManageVO tMallRxReportManageVO = new TMallRxReportManageVO();
		BeanUtils.copyProperties(tmrxreVo, tMallRxReportManageVO);
		tMallRxReportManageVO.setTargetName(targetNameList);
		tMallRxReportManageVO.setSaveTime(DvDateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
		if(StringUtils.isNotBlank(tmrxreVo.getUploadFileId())){
			String uploadFileName = reportImportStatusMapper.queryReportStatusByUUID(tmrxreVo.getUploadFileId()).getReportName();
			tmrxreVo.setUploadFileName(uploadFileName);
			tMallRxReportManageVO.setUploadFileName(tmrxreVo.getUploadFileName());
		}
		if(StringUtils.isNotBlank(reportid)){
			DvReportExportStatus dvReportExportStatus = new DvReportExportStatus();
			dvReportExportStatus.setReportid(reportid);
			dvReportExportStatus.setReportName(tmrxreVo.getReportName());
			dvReportExportStatus.setReportparam(JSON.toJSONString(tmrxreVo));
			dvReportExportStatus.setReportManageParam(JSON.toJSONString(tMallRxReportManageVO));
			if(reportExportStatusMapper.updateReportStatus(dvReportExportStatus)>0){
				return ServerResponse.createBySuccessMessage("修改报表成功!");
			}else{
				return ServerResponse.createBySuccessMessage("修改报表失败!");
			}
		}else{
			//加入报表前需要根据用户ID，报表名去判断是否有重名的报表
			if(reportExportStatusMapper.queryReport(tmrxreVo.getUserid(), tmrxreVo.getReportName())>0){
				return ServerResponse.createByErrorMessage("已存在相同名称的报表，请重新命名!");
			}else{
				reportid = reporttype+String.valueOf(System.currentTimeMillis())+String.valueOf((int)((Math.random()*9+1)*100000));
				tmrxreVo.setReportid(reportid);
				tMallRxReportManageVO.setReportid(reportid);
				DvReportExportStatus dvReportExportStatus = new DvReportExportStatus();
				BeanUtils.copyProperties(tmrxreVo, dvReportExportStatus);
				dvReportExportStatus.setCreatetime(new Date());
				dvReportExportStatus.setReportparam(JSON.toJSONString(tmrxreVo));
				dvReportExportStatus.setReportManageParam(JSON.toJSONString(tMallRxReportManageVO));
				dvReportExportStatus.setReporttype(reporttype);
				dvReportExportStatus.setFileName(tmrxreVo.getUploadFileId()+tmrxreVo.getUploadFileName());
				if(reportExportStatusMapper.insertReportStatus(dvReportExportStatus)>0){
					return ServerResponse.createBySuccessMessage("添加报表成功!");
				}else{
					return ServerResponse.createByErrorMessage("添加报表失败!");
				}
			}
		}
	}
	
	
	//更小时更新
	public void updateReport(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, 0);
		String endDate = DvDateUtils.format(calendar.getTime(),"yyyy-MM-dd HH:mm");
		calendar.add(Calendar.HOUR_OF_DAY, -1);
		String beginDate = DvDateUtils.format(calendar.getTime(),"yyyy-MM-dd HH:mm");
		
		List<DvReportExportStatus> dvReportExportStatusList = reportExportStatusMapper.queryMyReportByReportType(reporttype);
		for(DvReportExportStatus dvReportExportStatusItem : dvReportExportStatusList){
			TMallRxReportExportVO rmrxreVo = JSON.parseObject(dvReportExportStatusItem.getReportparam(), TMallRxReportExportVO.class);
			if("subscription".equals(rmrxreVo.getFetchType())){
				rmrxreVo.setBeginDate(beginDate);
				rmrxreVo.setEndDate(endDate);
				addOrUpdateReport(rmrxreVo);
			}
		}
	}
}
